import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { queryCommonType } from "@/utils/queryCommonType";

export const getCommonType = createAsyncThunk(
  "common/getCommonType",
  async (type) => {
    const res = await queryCommonType(type);
    return { data: res, type };
  }
);



const commonSlice = createSlice({
  name: "common",
  initialState: {
    tagType: localStorage.getItem("tagType") || [],
    cateType: localStorage.getItem("cateType") || [],
    userInfo:localStorage.getItem('userInfo')||{},
  },
  reducers: {},
  extraReducers(builder) {
    builder
      .addCase(getCommonType.pending, (state) => {
        console.log("🚀 ~ 进行中！");
      })
      .addCase(getCommonType.fulfilled, (state, { payload }) => {
        console.log("fulfilled", payload);
        state[payload.type] = payload.data;
        state.totals = payload.length;
      })
      .addCase(getCommonType.rejected, (state, err) => {
        console.log("🚀 ~ rejected", err);
      });
  },
});
